<?php

//引入自动加载文件
require 'vendor/autoload.php';
require 'mysql.class.php';
/**
 * 目前已经有：HTTP操作、多线程、模拟登陆等QueryList扩展
 * 下面来利用QueryList扩展来组合上面的例子，实现多线程采集文章并保存文章图片到本地
 */
use QL\QueryList;

// 永不过期
set_time_limit(0);

$mysql_obj = new lib_mysqli('localhost', 'root', 'root', 'book', '3306');



$datas = QueryList::Query('http://book.jd.com/booksort.html',array(
    'link' => array('a','href','',function($content){
        //利用回调函数补全相对链接
        $baseUrl = 'http:';
        return $baseUrl.$content;
    })),'.main .mc dl dd:eq(0) em')->getData(function($item){
    return $item['link'];
    });

$page = 'http://book.jd.com/booksort.html';
$contents = file_get_contents($page);



// 书类别
$data_book = QueryList::Query($contents,array(
    'link' => array('a','text','')),'.main .mc dl dt')->getData(function($item){
    return $item['link'];
});


// 查询 获取类目
header("Content-type:text/html;charset=utf-8");

$sql= 'SELECT * FROM jd_catebook WHERE id BETWEEN 3476 and 3572';
$result_book = $mysql_obj->get_all($sql);

//print_r($result_book);
//exit;
// 获取分类下书的链接
foreach($result_book as $v){
    //HTTP操作扩展
    $urls = QueryList::run('Request',[
        'target' => $v['catebook_url'],
        'referrer'=>'http://list.jd.com',
        'method' => 'GET',
        'params' => ['var1' => 'testvalue', 'var2' => 'somevalue'],
        'user_agent'=>'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0',
        'cookiePath' => './cookie.txt',
        'timeout' =>'60'
    ])->setQuery(['link' => ['div.gl-i-wrap div.p-name>a','href','',function($content){
        return 'http:'.$content;
//    }]],'#plist .gl-warp li:lt(40)')->getData(function($item){
    }]],'#plist .gl-warp li')->getData(function($item){
        return $item['link'];
    });
    for($i=0; $i<count($urls); $i++){
        $info = array( 'pid' => $v['id'], 'artibook_url'=> $urls[$i]  );
        $sql_new = $mysql_obj->get_insert_db_sql('jd_catebook', $info);
        $rs = $mysql_obj->query($sql_new);
        if($rs){
            echo $i;
        }
    }
}



//print_r($arr);
exit;




//print_r($arr);exit;
//多线程扩展

foreach($arr as $k => $v){
    for($j=0; $j<count($v); $j++){
        QueryList::run('Multi',[
            'list' => $v[$j],
            'curl' => [
                'opt' => array(
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_AUTOREFERER => true,
                ),
                //设置线程数
                'maxThread' => 100,
                //设置最大尝试数
                'maxTry' => 3
            ],
            'success' => function($a){
                //采集规则
                $reg = array(
                    //采集文章标题
                    'title' => array('#name h1','text'),
                    'title1' => array('#parameter2 li:eq(1)','text')

                );
                $rang = 'body';
                $ql = QueryList::Query($a['content'],$reg, $rang, 'GBK');
                $data = $ql->getData();
                //打印结果，实际操作中这里应该做入数据库操作
                print_r($data);
            }
        ]);
    }
}



